package com.edu.dao;

import com.edu.entity.User;
import com.edu.utils.JdbcHelper;

import java.sql.SQLException;
import java.util.List;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/18 14:09
 */
public class UserDao {
    // 添加
    public void insert(User user) throws SQLException {
        String sql = "insert into user(username,password,ident) values(?,?,?)";
        JdbcHelper.update(sql,user.getUsername(),user.getPassword(),0);
    }

    // 登录查询
    public User login(String username) throws SQLException {
        String sql = "select * from user where username=?";
        List<User> user = JdbcHelper.query(sql, User.class, username);
        if(user.size() > 0){
            return user.get(0);
        }
        return null;
    }
    // 查找普通用户
    public List<User> selectByOrdinary() throws SQLException{
        String sql = "select * from user where ident = 0";
        List userlist = JdbcHelper.query(sql, User.class);
        return userlist;
    }

    public User findOne(String username) throws SQLException {
        String sql = "select * from user where username = ?";
        List<User> users = JdbcHelper.query(sql, User.class, username);
        if(users.size() > 0 ){
            return users.get(0);
        }
        return null;
    }

    public User findOne(Integer id) throws SQLException {
        String sql = "select * from user where id = ?";
        List<User> users = JdbcHelper.query(sql, User.class, id);
        if(users.size() > 0 ){
            return users.get(0);
        }
        return null;
    }
}
